<?php

/**
 * web端返回信息
 * @param string $status
 * @param string $message
 * @param string $url
 * @return array
 */
function webResponse($status = 'SUCCESS', $message = '操作成功', $url = '')
{

    $response = [
        'status'  => $status,
        'message' => $message,
        'url'     => $url
    ];

    echo json_encode($response);
    die;
}

/**
 * api端返回信息
 * @param array $data
 * @return array
 */
function apiResponse(array $data = [])
{

    $q = [
        's' => 0,
        'd' => '操作成功',
        't' => date('Y-m-d H:i:s', time())
    ];
    $q = array_merge($q, $data);
    $action = request()->module;
    $response = [
        'n' => $action,
        's' => request()->s,
        'q' => $q
    ];


    /**
     * 将响应输出到日志文件（保存路：/storage/app/public/logs）
     */
    if (config('app.debug')) {
        $log = '---------' . PHP_EOL . $action . PHP_EOL . date('Y-m-d H:i:s') . PHP_EOL;
        Storage::disk('public')->append('logs\api_' . date('Ymd') . '.log', $log . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL);
    }

    return json_decode(str_replace('null','""',json_encode($response)),true);
}

//http-get请求函数
function curl_get($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //不检验证书，在linux下打开
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

//获得32位随机字符串
function getRandStr()
{
    $chars = implode('', array_merge(range('a', 'z'), range('A', 'Z')));
    $str = str_shuffle($chars);

    return substr($str, 0, 32);
}
    